package com.kaibai.dao.impl;

import com.kaibai.dao.BaseDao;
import com.kaibai.dao.GoodDao;
import com.kaibai.pojo.Good;

import java.sql.Connection;
import java.util.List;

public class GoodDaoImpl extends BaseDao implements GoodDao {
    @Override
    public int insert(Connection conn, Good good) {
        String sql = "insert into fruit(name,price,sales,stock,img_path) values(?,?,?,?,?)";
        return update(conn,sql,good.getName(),good.getPrice(),good.getSales(),good.getStock(),good.getImgPath());
    }

    @Override
    public int deleteById(Connection conn, int id) {
        String sql = "delete from fruit where id=?";
        return update(conn,sql,id);
    }

    @Override
    public int update(Connection conn, Good good) {
        String sql = "update fruit set name=? , price=? , sales=? , stock=? , img_path=? where id=?";
        return update(conn,sql,good.getName(),good.getPrice(),good.getSales(),good.getStock(),good.getImgPath(),good.getId());
    }

    @Override
    public Good getGoodById(Connection conn, int id) {
        String sql = "select id,name,price,sales,stock,img_path as imgPath from fruit where id=?";
        return getInstance(conn, Good.class,sql,id);
    }

    @Override
    public List<Good> getAll(Connection conn) {
        String sql = "select id,name,price,sales,stock,img_path as imgPath from fruit";
        return getForList(conn, Good.class,sql);
    }

    @Override
    public Integer getForPageTotalCount(Connection conn) {
        String sql = "select count(*) from fruit";
        Number number =  getValue(conn,sql);
        return number.intValue();
    }

    @Override
    public List<Good> getForPageItems(Connection conn, int begin, int pageSize) {
        String sql = "select id,name,price,sales,stock,img_path as imgPath from fruit limit ?,?";
        return getForList(conn,Good.class,sql,begin,pageSize);
    }

    @Override
    public Integer getForPageTotalCountByPrice(Connection conn, int min, int max) {
        String sql = "select count(*) from fruit where price between ? and ?";
        Number number =  getValue(conn,sql,min,max);
        return number.intValue();
    }

    @Override
    public List<Good> getForPageItemsByPirce(Connection conn, int begin, int pageSize, int min, int max) {
        String sql = "select id,name,price,sales,stock,img_path as imgPath from fruit where price between ? and ? order by price limit ?,?";
        return getForList(conn,Good.class,sql,min,max,begin,pageSize);
    }

    @Override
    public Integer getForPageTotalCountByName(Connection conn, String name) {
        String sql = "select count(*) from fruit where name like \"%\"?\"%\"";
        Number number = getValue(conn,sql,name);
        return number.intValue();
    }

    @Override
    public List<Good> getForPageItemsByName(Connection conn, int begin, int pageSize, String name) {
        String sql = "select id,name,price,sales,stock,img_path as imgPath from fruit where name like \"%\"?\"%\" order by price limit ?,?";
        return getForList(conn,Good.class,sql,name,begin,pageSize);
    }

    @Override
    public Integer getForPageTotalCountBySearch(Connection conn, String name, int min, int max) {
        String sql = "select count(*) from fruit where name like \"%\"?\"%\" and price between ? and ?";
        Number number = getValue(conn,sql,name,min,max);
        return number.intValue();
    }

    @Override
    public List<Good> getForPageItemsBySearch(Connection conn, int begin, int pageSize, String name, int min, int max) {
        String sql = "select id,name,price,sales,stock,img_path as imgPath from fruit where name like \"%\"?\"%\" and price between ? and ? order by price limit ?,?";
        return getForList(conn,Good.class,sql,name,min,max,begin,pageSize);
    }

}
